package com.ophiux.FeverManagement.infratructure.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.FeverManagement.domain.entity.DoctorInfo;
import com.ophiux.FeverManagement.domain.entity.RbPatientinfo;
import com.ophiux.FeverManagement.domain.repository.IPatientRepositories;
import com.ophiux.FeverManagement.infratructure.mapper.RbPatientinfoMapper;
import com.ophiux.utils.common.StringUtil;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;
@Repository
@Slf4j
public class PatientRepositoriesImpl implements IPatientRepositories{
	@Autowired
	RbPatientinfoMapper patientinfoMapper;
	
	@Override
	public String save(RbPatientinfo model) throws OphiuxRepositoryException {
		String id = "";
		try {
			if(StringUtil.isEmpty(model.getPATIENT_ID())) {//新增
				id =  UUID.randomUUID().toString().replaceAll("-", "");
				model.setPATIENT_ID(id);
				patientinfoMapper.insert(model);
			}else {
				id = model.getPATIENT_ID();
				patientinfoMapper.update(model);
			}
		}catch (Exception e) {
			e.printStackTrace();
			log.info("PatientRepositoriesImpl=>save(),数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException("PatientRepositoriesImpl=>save(),数据库执行异常");
		}
		return id;
	}

	@Override
	public void delete(Object uniqueId) throws OphiuxRepositoryException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public RbPatientinfo selectOneByKey(Object uniqueId) throws OphiuxRepositoryException {
		try {
			RbPatientinfo model = patientinfoMapper.selectOneByKey(uniqueId);
			return model;
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public RbPatientinfo selectOne(Map map) throws OphiuxRepositoryException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RbPatientinfo> selectAll(Map map) throws OphiuxRepositoryException {
		List<RbPatientinfo> patientList = new ArrayList<>();
		try {
			patientList = patientinfoMapper.selectAll(map);
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("PatientRepositoriesImpl=>selectAll(),数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
		return patientList;
	}

	@Override
	public RbPatientinfo getPatientByIdNo(String idNo) throws OphiuxRepositoryException {
		RbPatientinfo patient = null;
		if(StringUtil.isEmpty(idNo)) {//新增
			return patient;
		}
		try {
			patient = patientinfoMapper.getPatientByIdNo(idNo);
		}catch (Exception e) {
			e.printStackTrace();
			log.info("PatientRepositoriesImpl=>getPatientByIdNo(),数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException("PatientRepositoriesImpl=>getPatientByIdNo(),数据库执行异常");
		}
		return patient;
	}

}
